/**
 * Copyright (c) 2002-2012 旭鸣信息
 * All rights reserved. 
 * File：FrontUserMessageDaoImpl.java
 * History: Dec 27, 2012: Initially created, liuzhi.
 */
package com.xteam.ecsystem.dao.Impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.xteam.ecsystem.dao.IFrontUserMessageDao;
import com.xteam.ecsystem.entities.BaseReplyInfo;
import com.xteam.ecsystem.entities.BaseUserMessage;
import com.xteam.ecsystem.utils.PageCenterStation;

/**
 * 前端用户留言数据访问实现类
 * @author liuzhi
 *
 */
@Repository
public class FrontUserMessageDaoImpl extends DDLPrefix implements IFrontUserMessageDao {
	
	@SuppressWarnings("unchecked")
	@Autowired
	private BaseDaoImpl baseDao;

	@SuppressWarnings("unchecked")
	public PageCenterStation<?> getUserMessages(String userEmail, int pageIndex, int pageSize) {
		try {
			List list = new ArrayList();
			
			String sql = "SELECT bum.bum_id AS bumId, bum.bum_name AS bumName, bum.bum_email AS bumEmail, " 
					+ "bum.bum_type AS bumType, bum.bum_title AS bumTitle, bum.bum_content AS bumContent, " 
					+ "bum.bum_attachname AS bumAttachname, bum.bum_attachurl AS bumAttachurl, " 
					+ "bum.bum_ip AS bumIp, bum.bum_time AS bumTime, bum.bum_state AS bumState, " 
					+ "bri.bri_content AS replyContent, bri.bri_time as replyTime " 
					+ "FROM base_user_message AS bum " 
					+ "LEFT JOIN base_reply_info AS bri ON bum.bum_id = bri.bri_target " 
					+ "WHERE bum.bum_email = ? ORDER BY bum.bum_time ASC"; 
			
			String countSql = "SELECT COUNT(*) FROM base_user_message WHERE bum_email = ?";
			
			list.add(userEmail);
			
			PageCenterStation<?> p = (PageCenterStation<?>) this.baseDao.
					queryWithSQL(BaseUserMessage.class, countSql, sql, null,
							list.toArray(), (pageIndex - 1) * pageSize, pageSize);
			
			
			return p;
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}

	public void delUserMessage(String bumId) {
		try {
			String hql = SQL_DELETE + SQL_FROM + BaseUserMessage.REF + SQL_WHERE + "bumId = ?";
			
			this.baseDao.executeUpdate(hql, new Object[]{bumId});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void delUserReMessage(String bumId) {
		try {
			String hql = SQL_DELETE + SQL_FROM + BaseReplyInfo.REF + SQL_WHERE + "briTarget = ?";
			
			this.baseDao.executeUpdate(hql, new Object[]{bumId});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void saveUserMsg(BaseUserMessage userMsg){
		try {
			this.baseDao.save(userMsg);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
